<template>
	<el-dialog :visible.sync="visible" :modal-append-to-body="false" title="修改密码" class="demo-ruleForm">
		<el-form :model="model" :rules="rules" ref="modelForm" label-width="100px" @submit.native.prevent
			:inline="false">
			<el-form-item label="当前密码" prop="oldPass">
				<el-input type="password" v-model="model.oldPass" auto-complete="off"></el-input>
			</el-form-item>
			<el-form-item label="新密码" prop="pass">
				<el-input type="password" v-model="model.pass" auto-complete="off"></el-input>
			</el-form-item>
			<el-form-item label="确认密码" prop="checkPass">
				<el-input type="password" v-model="model.checkPass" auto-complete="off"></el-input>
			</el-form-item>
		</el-form>
		<div slot="footer" class="dialog-footer">
			<el-button @click="close">取 消</el-button>
			<el-button type="primary" @click="save" :loading="loading">确 定</el-button>
		</div>
	</el-dialog>
</template>

<script>
	import common from "@/common/common.js"

	export default {
		name: "updatePwd",
		data() {
			var self = this;
			return {
				visible: false,
				loading: false,
				//密码
				model: {
					oldPass: '',
					pass: '',
					checkPass: ''
				},
				rules: {
					oldPass: [{
						required: true,
						message: '请输入当前密码',
						trigger: 'blur'
					}],
					pass: [{
							required: true,
							message: '请输入新密码',
							trigger: 'blur'
						},
						{
							validator(rule, value, callback) {
								if (value == null || value == "") {
									callback(new Error('请输入新密码'));
								} else {
									callback();
								}
							},
							trigger: 'blur'
						}
					],
					checkPass: [{
						validator(rule, value, callback) {
							if (value == null || value == "") {
								callback(new Error('请再次输入密码'));
							} else if (value !== self.model.pass) {
								callback(new Error('两次输入密码不一致!'));
							} else {
								callback();
							}
						},
						trigger: 'blur'
					}]
				},
			}
		},
		methods: {
			show() {
				this.visible = true;
			},
			close() {
				this.visible = false;
			},
			save() {
				this.$refs["modelForm"].validate(
					(valid) => {
						if (valid) {
							var model = {
								oldPass: common.tool.md5(this.model.oldPass),
								pass: common.tool.md5(this.model.pass)
							};
							var post = common.tool.toKeyValue(model);
							this.loading = true;
							common.tool.post("/api/System/UpdatePwd", post).then((data) => {
								common.showToast(data.Message, "success");
								this.visible = false;
								this.loading = false;
							}).catch(() => {
								this.loading = false;
							});
						} else {}
					}
				);
			}
		}
	}
</script>
